package com.itboye.ihomebank.activity.keytwo.fragment;

import android.widget.ListView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.AdminPassAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.base.ptr.BasePtr;
import com.itboye.ihomebank.bean.GuanLiPassBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyTwoPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
/**
 * 密码发送记录-白马锁
 */
public class AdminPassFragment extends BaseFragment implements Observer{
    ListView listView;
    PtrFrameLayout commend_anchor_ptr;

    KeyTwoPresenter presenter;
    private GuanLiPassBean bean;

    List<GuanLiPassBean.Date> beanList;
    AdminPassAdapter adapter;

    private String uid;
    public static  String lockId,lockMac;

    int current_page=1;
    int per_page=10;
    String sid;
    public static boolean addLock = false;
    @Override
    public int initView() {
        return R.layout.fragment_guan_li_pass;
    }

    @Override
    public void onMyActivityCreated() {
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
        lockId = getActivity().getIntent().getStringExtra("lockid");
        lockMac= getActivity().getIntent().getStringExtra("lockMac");
        presenter=new KeyTwoPresenter(this);
        bean=new GuanLiPassBean();
        beanList=new ArrayList<>();
        sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";

        if ("".equals(sid)){
            presenter.onPassJiLu(uid,lockId,current_page+"",per_page+"");

        }else {
            presenter.onPassJiLu(sid,lockId,current_page+"",per_page+"");

        }
        adapter = new AdminPassAdapter(getActivity(), beanList,R.layout.item_yoghu_pass);
        listView.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";

                current_page++;
                if ("".equals(sid)){
                    presenter.onPassJiLu(uid,lockId,current_page+"",per_page+"");

                }else {
                    presenter.onPassJiLu(sid,lockId,current_page+"",per_page+"");

                }
            }
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
                if ("".equals(sid)){
                    presenter.onPassJiLu(uid,lockId,current_page+"",per_page+"");

                }else {
                    presenter.onPassJiLu(sid,lockId,current_page+"",per_page+"");

                }
            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == presenter.pass_jilu_success) {
                if (current_page == 1) {
                    beanList.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                bean= (GuanLiPassBean) entity.getData();
                if (bean!=null){
                    beanList.addAll(bean.getData());
                    if (beanList.size() > 0) {
                        adapter.notifyDataSetChanged();
                    }
                }
            }else if (entity.getEventType() == presenter.pass_jilu_fail) {
                if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
                    //切换 锁备用服务器
                    showAlert();
                }else {
                    ByAlert.alert(entity.getData());
                }
            }else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
                SIDBean data1 = (SIDBean) entity.getData();
                SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
                SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
                presenter.onPassJiLu(data1.getSid(),lockId,current_page+"",per_page+"");
            }else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
                ByAlert.alert(entity.getMsg());
            }
        }
    }
}
